/**
 * @author 董一华
 * @since 1.00
 * @version 1.00
 * 
 * 移动各级组织下拉框控件
 * 使用说明：
 * jsp中使用范例(最短模式)：<htdz-tag:SelectMobileTag areaid="${xxx}" county="${xxx}" hall="${xxx}"/>
 * 参数说明：
 * 1. areaid为可选项，是地市下拉框的selected值
 * 2. county为可选项，是区移动下拉框的selected值
 * 3. hall为可选项，是服务厅下拉框的selected值   
 * 4. onchange_hall为可选项。为hall下拉框指定onchange方法
 * 5. firstoption为首个下拉框的text。可以不写，缺省值为"全部"。如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则不需要添加额外的option
 * 6. orgatypeid为可选项，为调用此tag的用户所属组织类型，一般为session值，缺省值为MyConstant.ORGATYPE_PROVMOBILE
 * 7. orgacode为可选项，为调用此tag的用户所属组织编码，一般为session值。如果orgatype为平台，则orgacode为选填项
 * 8. hidehall为可选项，是否隐藏"服务厅"下拉框。默认值为false
 * 9. channeltype为hall取值范围，可选项，缺省值为MyConstant.SELECT_FIRST_VALUE,即全部渠道；(当同一页面中包含channeltype下拉框时，服务厅会随channeltype下拉框自动变化)
 * 10.DDflag是否只显示参与调度的服务厅、区/县，如果DDflag为true,则根据channeltype值，如果channeltype为空，则不分渠道类型所有参与调度的自有渠道服务厅
 * 11.zoneid：区域ID(地市下拉框以zoneid取值)
 * 
 * 修订列表： v1.00 2010-07-25 
 * 			修订人：董一华
 * 			修订内容：初始版本
 * 
 */

package com.zjht.gmcc.bean.tag;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParam;
import com.zjht.gmcc.bean.util.MyParamUtil;

public class SelectMobileTag extends TagSupport {
	private static final long serialVersionUID = 7343412093252888637L;
	private String zoneid;
	private String areaid;
	private String county;
	private String hall;
	private String channeltype;
	private String onchange_hall;
	private String firstoption;
	private String orgatypeid;
	private String orgacode;
	private boolean hidehall;
	private boolean DDflag;

	public SelectMobileTag() {
	}

	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {
		String zoneid = this.zoneid;
		String areaid = this.areaid;
		String county = this.county;
		String hall = this.hall;
		String channeltype = this.channeltype;
		String onchange_hall = this.onchange_hall;
		String firstoption = this.firstoption;
		String orgatypeid = this.orgatypeid;
		int orgtypeid = MyConstant.INT_NULL;
		String orgacode = this.orgacode;
		boolean hidehall = this.hidehall;
		boolean DDflag = this.DDflag;

		StringBuilder html = new StringBuilder();
		if (MyFormat.isStrNull(firstoption))
			firstoption = "全部";
		try {
			if (MyFormat.isStrNull(orgatypeid)) {
				orgtypeid = MyConstant.ORGTYPE_PROVMOBILE;
			} else {
				try {
					orgtypeid = Integer.valueOf(orgatypeid);
				} catch (Exception ex) {
					orgtypeid = MyConstant.ORGTYPE_PROVMOBILE;
				}
			}
			if (MyFormat.isStrNull(areaid)) {
				areaid = MyConstant.SELECT_FIRST_VALUE;
			}
			if (MyFormat.isStrNull(channeltype)) {
				channeltype = MyConstant.SELECT_FIRST_VALUE;
			}
			// 是否为省级组织
			boolean isProv = MyConstant.ORGTYPE_PROVMOBILE == orgtypeid || MyConstant.ORGTYPE_PROVPROVIDER == orgtypeid
					|| MyConstant.ORGTYPE_VENDOR == orgtypeid;

			html.append("地市：<select name=\"areaid\" id=\"areaid\"");
			if (isProv) {
				html.append(" onchange=\"clearSelect('hall');");
				html.append("getCountyFull(this.value,'");
				html.append(channeltype);
				html.append("','");
				html.append(DDflag);
				html.append("');\"");
			}
			html.append(">");

			if (!isProv) {
				html.append("<option value=\"");
				html.append(areaid);
				html.append("\">");
				html.append(MyParamUtil.getAreaName(areaid));
				html.append("</option>");
			} else {
				if (!MyConstant.SELECT_NO_FIRST_OPTION.equals(firstoption)) {// 如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则不需要添加额外的option
					html.append("<option value=\"");
					html.append(MyConstant.SELECT_FIRST_VALUE);
					html.append("\">");
					html.append(firstoption);
					html.append("</option>");
				}
				if (MyConstant.SELECT_FIRST_VALUE.equals(zoneid)) {

				} else if ((MyConstant.ORGTYPE_PROVPROVIDER == orgtypeid && !MyFormat.isStrNull(orgacode) && (!MyConstant.SELECT_FIRST_VALUE
						.equals(orgacode)))
						|| (MyConstant.ORGTYPE_PROVMOBILE == orgtypeid && !MyFormat.isStrNull(zoneid) && !MyConstant.SELECT_FIRST_VALUE
								.equals(zoneid))) {// 显示子组织下拉框或区域下面地市

					MyJDBC myJDBC = null;
					try {
						myJDBC = new MyJDBC();
						String strSQL = "";
						PreparedStatement ps = null;
						if (!MyFormat.isStrNull(orgacode)) {
							strSQL = "SELECT a.areaid,a.areaname FROM ORGANIZA o,AREA a WHERE o.parentorgacode=? AND a.areaid=o.areaid AND o.status='1' ORDER BY a.areaid";
							ps = myJDBC.prepareStatement(strSQL);
							ps.setString(1, orgacode);
						} else {
							strSQL = "SELECT areaid,areaname FROM AREA WHERE parentid=? ORDER BY areaid";
							ps = myJDBC.prepareStatement(strSQL);
							ps.setString(1, zoneid);
						}
						ResultSet rs = ps.executeQuery();
						String s_areaid = "";
						String s_areaname = "";
						while (rs.next()) {
							s_areaid = rs.getString(1);
							s_areaname = rs.getString(2);
							html.append("<option value=\"");
							html.append(s_areaid);
							html.append("\" ");
							if (s_areaid.equals(areaid))
								html.append("selected");
							html.append(">");
							html.append(s_areaname);
							html.append("</option>");
						}
						rs.close();
						rs = null;
					} catch (Exception e) {
						e.printStackTrace();
					} finally {
						MyJDBC.close(myJDBC);
					}
				} else if (MyParam.hAreaIDToAreaName != null && MyParam.hAreaIDToAreaName.size() > 0) {// 全部地市
					Iterator<Map.Entry<String, String>> iterator = MyParam.hAreaIDToAreaName.entrySet().iterator();
					String key = null;
					String value = null;
					Map.Entry<String, String> entry = null;
					while (iterator.hasNext()) {
						entry = iterator.next();
						key = entry.getKey();
						value = entry.getValue();
						html.append("<option value=\"");
						html.append(key);
						html.append("\" ");
						if (areaid != null && areaid.length() > 0 && key.equals(areaid))
							html.append("selected");
						html.append(">");
						html.append(value);
						html.append("</option>");
					}
				}
			}
			html.append("</select>&nbsp;");

			// 区移动下拉框county
			html.append("区/县：<select name=\"county\" id=\"county\"");
			if (!hidehall && !(MyConstant.ORGTYPE_COUNTYMOBILE == orgtypeid || MyConstant.ORGTYPE_HALL == orgtypeid)) {
				html.append(" onchange=\"getHallFull(this.value,'");
				html.append(channeltype);
				html.append("','");
				html.append(DDflag);
				html.append("');\"");
			}
			html.append(">");

			if (MyConstant.ORGTYPE_COUNTYMOBILE == orgtypeid || MyConstant.ORGTYPE_HALL == orgtypeid) {
				html.append("<option value=\"");
				html.append(county);
				html.append("\">");
				html.append(MyParamUtil.getOrgaShortName(county));
				html.append("</option>");
			} else {
				if (!MyConstant.SELECT_NO_FIRST_OPTION.equals(firstoption)) {// 如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则不需要添加额外的option
					html.append("<option value=\"");
					html.append(MyConstant.SELECT_FIRST_VALUE);
					html.append("\">");
					html.append(firstoption);
					html.append("</option>");
				}
				if (!MyFormat.isStrNull(areaid) && !MyConstant.SELECT_FIRST_VALUE.equals(areaid)) {
					MyJDBC myJDBC = null;
					try {
						myJDBC = new MyJDBC();
						StringBuilder sql = new StringBuilder();
						sql.append("SELECT orgacode,shortname FROM ORGANIZA c WHERE areaid=? AND orgatypeid=?");
						if (!MyConstant.SELECT_FIRST_VALUE.equals(channeltype) || DDflag) {
							sql.append(" AND EXISTS(SELECT 1 FROM ORGANIZA h WHERE h.parentorgacode=c.orgacode ");
							if (DDflag) {
								sql.append(" AND h.DDflag='1'");
							}
							if (!MyConstant.SELECT_FIRST_VALUE.equals(channeltype)) {
								sql.append(" AND h.channeltype=?");
							}
							sql.append(" )");
						}
						sql.append(" order by shortname");
						PreparedStatement ps = myJDBC.prepareStatement(sql);
						ps.setString(1, areaid);
						ps.setInt(2, MyConstant.ORGTYPE_COUNTYMOBILE);
						if (!MyConstant.SELECT_FIRST_VALUE.equals(channeltype)) {
							ps.setString(3, channeltype);
						}
						ResultSet rs = ps.executeQuery();
						String countycode = "";
						String countyname = "";
						while (rs.next()) {
							countycode = rs.getString(1);
							countyname = rs.getString(2);
							html.append("<option value=\"");
							html.append(countycode);
							html.append("\" ");
							if (!MyFormat.isStrNull(county) && county.equals(countycode))
								html.append("selected");
							html.append(">");
							html.append(countyname);
							html.append("</option>");
						}
						rs.close();
						rs = null;
					} catch (Exception e) {
						e.printStackTrace();
					} finally {
						MyJDBC.close(myJDBC);
					}
				}
			}
			html.append("</select>&nbsp;");

			// 服务厅下拉框hall
			if (!hidehall) {
				html.append("&nbsp;服务厅：<select name=\"hall\" id=\"hall\"");
				if (!MyFormat.isStrNull(onchange_hall)) {
					html.append(" onchange=\"");
					html.append(onchange_hall);
					html.append("\"");
				}
				html.append(">");

				if (MyConstant.ORGTYPE_HALL == orgtypeid) {
					html.append("<option value=\"");
					html.append(hall);
					html.append("\">");
					html.append(MyParamUtil.getOrgaShortName(hall));
					html.append("</option>");
				} else {
					if (!MyConstant.SELECT_NO_FIRST_OPTION.equals(firstoption)) {// 如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则不需要添加额外的option
						html.append("<option value=\"");
						html.append(MyConstant.SELECT_FIRST_VALUE);
						html.append("\">");
						html.append(firstoption);
						html.append("</option>");
					}
					if (!MyFormat.isStrNull(county) && !MyConstant.SELECT_FIRST_VALUE.equals(county)) {
						MyJDBC myJDBC = null;
						try {
							myJDBC = new MyJDBC();
							StringBuilder sql = new StringBuilder();
							sql.append("SELECT orgacode,shortname FROM ORGANIZA WHERE areaid=? AND orgatypeid=? AND parentorgacode=? ");
							if (!MyConstant.SELECT_FIRST_VALUE.equals(channeltype))
								sql.append(" AND channeltype=?");
							if (DDflag)
								sql.append(" AND DDFlag='1'");
							sql.append(" order by shortname");
							PreparedStatement ps = myJDBC.prepareStatement(sql);
							ps.setString(1, areaid);
							ps.setInt(2, MyConstant.ORGTYPE_HALL);
							ps.setString(3, county);
							if (!MyConstant.SELECT_FIRST_VALUE.equals(channeltype))
								ps.setString(4, channeltype);
							ResultSet rs = ps.executeQuery();
							String hallcode = "";
							String hallname = "";
							while (rs.next()) {
								hallcode = rs.getString(1);
								hallname = rs.getString(2);
								html.append("<option value=\"");
								html.append(hallcode);
								html.append("\" ");
								if (!MyFormat.isStrNull(hall) && hall.equals(hallcode))
									html.append("selected");
								html.append(">");
								html.append(hallname);
								html.append("</option>");
							}
							rs.close();
							rs = null;
						} catch (Exception e) {
							e.printStackTrace();
						} finally {
							MyJDBC.close(myJDBC);
						}
					}
				}
				html.append("</select>");
			}
			pageContext.getOut().print(html.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		html = null;
		return this.SKIP_BODY;
	}

	public void setAreaid(String areaid) {
		this.areaid = areaid;
	}

	public void setAreaid(int areaid) {
		this.areaid = String.valueOf(areaid);
	}

	public void setCounty(String county) {
		this.county = county;
	}

	public void setHall(String hall) {
		this.hall = hall;
	}

	public void setChanneltype(String channeltype) {
		this.channeltype = channeltype;
	}

	public void setOnchange_hall(String onchange_hall) {
		this.onchange_hall = onchange_hall;
	}

	public void setFirstoption(String firstoption) {
		this.firstoption = firstoption;
	}

	public void setOrgatypeid(String orgatypeid) {
		this.orgatypeid = orgatypeid;
	}

	public void setOrgatypeid(int orgatypeid) {
		this.orgatypeid = String.valueOf(orgatypeid);
	}

	public void setHidehall(boolean hidehall) {
		this.hidehall = hidehall;
	}

	public void setOrgacode(String orgacode) {
		this.orgacode = orgacode;
	}

	public void setDDflag(boolean DDflag) {
		this.DDflag = DDflag;
	}

	public void setDDflag(String DDflag) {
		this.DDflag = MyConstant.TRUE.equals(DDflag) || "true".equals(DDflag);
	}

	public void setZoneid(String zoneid) {
		if (MyFormat.isStrNull(zoneid))
			zoneid = MyConstant.SELECT_FIRST_VALUE;
		this.zoneid = zoneid;
	}

	public void setZoneid(int zoneid) {
		this.zoneid = String.valueOf(zoneid);
	}
}
